package com.gopay.common.rcs.service.translimit.impl;

import com.gopay.common.constants.proccode.ProcCodeEnum;
import com.gopay.common.constants.rcs.TranDirection;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.domain.acps.AcctProd;
import com.gopay.common.domain.rcs.req.RcsTransLimitRequest;
import com.gopay.common.domain.rcs.res.RcsTransLimitResponse;
import com.gopay.common.exception.GopayException;
import com.gopay.common.rcs.manager.translimit.RcsBAcctLimitStatisticsManager;
import com.gopay.common.rcs.manager.translimit.RcsGAcctLimitStatisticsManager;
import com.gopay.common.rcs.service.translimit.RcsModifyTransLimitService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 修改额度
 * 
 * @RcsModifyTransLimitService.java
 * @author liujy
 * @2013-7-22 下午2:30:57 www.gopay.com.cn Inc.All rights reserved.
 */
@Service("rcsModifyTransLimitService")
public class RcsModifyTransLimitServiceImpl implements RcsModifyTransLimitService {

    @Resource(name = "rcsGAcctLimitStatisticsManager")
    private RcsGAcctLimitStatisticsManager rcsGAcctLimitStatisticsManager;
    @Resource(name = "rcsBAcctLimitStatisticsManager")
    private RcsBAcctLimitStatisticsManager rcsBAcctLimitStatisticsManager;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class, RuntimeException.class })
    public RcsTransLimitResponse addTranLimit(RcsTransLimitRequest request) throws GopayException {
        return updateTranLimit(request);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class, RuntimeException.class })
    public RcsTransLimitResponse subtractTranLimit(RcsTransLimitRequest request) throws GopayException {
        return updateTranLimit(request);
    }

    /**
     * 
     * @param request
     * @param isAddLimit
     * @return   
     * @throws GopayException
     */
    private RcsTransLimitResponse updateTranLimit(RcsTransLimitRequest request)
            throws GopayException {

        RcsTransLimitResponse response = new RcsTransLimitResponse();

        String custId = request.getCustId();
        AcctProd acctProd = request.getAcctProd();
        IntTxnCd intTxnCd = request.getIntTxnCd();
        TranDirection tranDirection = request.getTranDirection();
        String tranDate = request.getTransDate();
        BigDecimal tranAmt = request.getTranAmts().get(0);
        
        rcsGAcctLimitStatisticsManager.saveOrUpdateTransLimitStatistics(custId, acctProd, intTxnCd, tranDirection,
                tranDate, tranAmt, request.getRequestType());
        if (IntTxnCd._00700.equals(intTxnCd)) {
            rcsBAcctLimitStatisticsManager.saveOrUpdateTransLimitStatistics(custId, acctProd, intTxnCd, tranDirection,
                    tranDate, tranAmt, request.getPbocBankCode(), request.getBankAcctNum(), request.getBankName(),
                    request.getBankAcctName(), request.getRequestType());
        }

        response.setProcCode(ProcCodeEnum.PROC_CODE_200S1000.getCode());

        return response;
    }
}